package com.healthcloud.controller;

import java.util.ArrayList;
import java.util.List;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.entity.TestprojectitemEntity;
import com.healthcloud.service.TestprojectitemService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.entity.TestreferencevalueEntity;
import com.healthcloud.service.TestreferencevalueService;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 项目参考值
 *
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2017-11-15 09:05:05
 */
@ApiIgnore
@Controller
@RequestMapping("testreferencevalue")
public class TestreferencevalueController {
	@Autowired
	private TestreferencevalueService testreferencevalueService;

	@Autowired
    private TestprojectitemService testprojectitemService;
	/**
	 * 列表
	 */
	@RequestMapping(value="/toTestreferencevalue",method = RequestMethod.GET)
	public String totestreferencevalue(){
		return "bus/testreferencevalue/testreferencevalue";
	}

    /**
     * 显示
     */
	@RequestMapping(value = "/searchTestreferencevalue")
	@ResponseBody
    public MsgVo searchTestreferencevalue(TestreferencevalueEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){
        List<TestreferencevalueEntity> list = testreferencevalueService.queryList(bean, pageParam);
        int count = testreferencevalueService.queryTotal(bean);
        if(list != null) {
            for(TestreferencevalueEntity testreferencevalueEntity : list) {
                if(testreferencevalueEntity.getTestItemName() == null) {
                    //设置每个项目编号对应的项目名
                    TestprojectitemEntity testprojectitemEntity = testprojectitemService.queryObject(testreferencevalueEntity.getTestItemCode());
                    if(testprojectitemEntity != null) {
                        testreferencevalueEntity.setTestItemName(testprojectitemEntity.getTestItemName());
                    }else {
                        testreferencevalueEntity.setTestItemName("");
                    }
                }
            }
            if( !bean.getTestItemName().equals("") && bean.getTestItemName() != null) {
                List<TestprojectitemEntity> testprojectitemEntities = testprojectitemService.queryByName(bean.getTestItemName());
                List<TestreferencevalueEntity> entitylist = new ArrayList<>();
                if(testprojectitemEntities != null) {
                    List<String> code = new ArrayList<>();
                    for(TestprojectitemEntity testprojectitemEntity : testprojectitemEntities) {
                        code.add(testprojectitemEntity.getTestItemCode());
                    }
                    entitylist = testreferencevalueService.queryByCode(code);
                    if(entitylist != null) {
                        for(TestreferencevalueEntity entity : entitylist) {
                            if(entity.getTestItemName() == null) {
                                TestprojectitemEntity testprojectitemEntity = testprojectitemService.queryObject(entity.getTestItemCode());
                                if(testprojectitemEntity != null) {
                                    entity.setTestItemName(testprojectitemEntity.getTestItemName());
                                }
                            }
                       }
                    }
                    count = entitylist.size();
                    return new MsgVo(entitylist,count,count,pageParam.getsEcho());
                }else {
                    count = entitylist.size();
                    return new MsgVo(entitylist,count,count,pageParam.getsEcho());
                }
            }
        }
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }

	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
	@RequiresPermissions("testreferencevalue:save")
    public String toSave(Model model) {
        return "bus/testreferencevalue/addOrUpdatetestreferencevalue";
    }

	/**
	 * 保存
	 */
    @SysLog("保存Testreferencevalue")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(TestreferencevalueEntity entity, String testItemCode) {

        TestprojectitemEntity testprojectitemEntity = testprojectitemService.queryObject(testItemCode);
        entity.setTestItemCode(testItemCode);
        entity.setTestItemName(testprojectitemEntity.getTestItemName());
        testreferencevalueService.save(entity);
        return ApiRes.SUCCESS;
    }

	/**
	 * 修改页
	 */
	@RequestMapping("/toupdate")
    @RequiresPermissions("testreferencevalue:update")
    public String toUpdate(@Param("id") Integer id, Model model) {

        TestreferencevalueEntity entity = testreferencevalueService.queryObject(id);
        model.addAttribute("entity", entity);
        TestprojectitemEntity testprojectitemEntity = testprojectitemService.queryObject(entity.getTestItemCode());
        model.addAttribute("testprojectItem",testprojectitemEntity);
        return "bus/testreferencevalue/addOrUpdatetestreferencevalue";
    }

    /**
     * 修改
     */
    @SysLog("修改Testreferencevalue")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(TestreferencevalueEntity entity, String testItemCode) {

        entity.setTestItemCode(testItemCode);
        testreferencevalueService.update(entity);
        TestprojectitemEntity testprojectitemEntity = testprojectitemService.queryObject(testItemCode);
        entity.setTestItemName(testprojectitemEntity.getTestItemName());
        return ApiRes.SUCCESS;
    }


    /**
     * 删除
     */
    @SysLog("删除Testreferencevalue")
    @RequestMapping("/delete")
    @RequiresPermissions("testreferencevalue:delete")
    @ResponseBody
    public ApiRes delete(Integer[] ids) {
        testreferencevalueService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }

}
